package com.liao.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.view.R;
import com.liao.view.domain.VideoInfo;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.jzvd.JzvdStd;
import de.hdodenhof.circleimageview.CircleImageView;

public class VideoListViewAdapter extends BaseAdapter {
    Context context;
    List<VideoInfo.ItemListBean> mDatas;

    public VideoListViewAdapter(Context context, List<VideoInfo.ItemListBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_video_layout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        VideoInfo.ItemListBean.DataBean dataBean = mDatas.get(position).getData();
        String picUrl = dataBean.getCover().getFeed();  //显示的大图
        holder.jzvdStd.setUp(dataBean.getPlayUrl(), dataBean.getTitle());
        Picasso.get().load(picUrl).into(holder.jzvdStd.posterImageView);
        holder.jzvdStd.positionInList = position;
//        获取提供者信息
        VideoInfo.ItemListBean.DataBean.ProviderBean provider = dataBean.getProvider();
        holder.authorTv.setText(provider.getName());
        String iconUrl = provider.getIcon();
        if (!TextUtils.isEmpty(iconUrl)) {
            Picasso.get().load(iconUrl).into(holder.iconIv);
        }
//       获取此视频的数据信息
        VideoInfo.ItemListBean.DataBean.ConsumptionBean consumption = dataBean.getConsumption();
        holder.loveTv.setText(String.valueOf(consumption.getCollectionCount()));
        holder.replyTv.setText(String.valueOf(consumption.getReplyCount()));
        return convertView;
    }

    class ViewHolder {
        JzvdStd jzvdStd;
        TextView authorTv, replyTv, loveTv;
        CircleImageView iconIv;

        public ViewHolder(View view) {
            jzvdStd = view.findViewById(R.id.item_video_jzvd);
            iconIv = view.findViewById(R.id.item_video_iv_icon);
            authorTv = view.findViewById(R.id.item_video_tv_author);
            replyTv = view.findViewById(R.id.item_video_tv_reply);
            loveTv = view.findViewById(R.id.item_video_tv_love);
        }
    }
}
